#!/usr/bin/python
###################################################################################################                                                                                              
#                                                                                              
# DEVELOPERS : Luca Sanna                                                                        
# VERSIONE : 0.1                                                                                  
#                                                                                                                    
# E-MAIL: sanluca78@gmail.com                                                                                        
# COPYRIGHT: (c) 2015                                                                      
# PROGRAMMA: Py-Home-Slave  
#This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.                                                                          
###################################################################################################
import time,sys,os
from threading import Thread
from baseconfig import *

sys.path.append("%slib" %workingDir)

from tools import *
from database import *
from ablib import Pin

class MyButton(Thread):
    time.sleep(1)
    db=Mydata()
    debouncingtime=1
    gpio=0
    prectime=0
    precenel=0
    precimp=0
    z=0
    p=0
    a=0
    
    in1 = Pin('J4.11','INPUT')
    in2 = Pin('J4.13','INPUT')
    in3 = Pin('J4.15','INPUT')
    in4 = Pin('J4.17','INPUT')
    in5 = Pin('J4.19','INPUT')
    in6 = Pin('J4.21','INPUT')
    in7 = Pin('J4.23','INPUT')
    in8 = Pin('J4.25','INPUT')
    
    def actualTime(self):
        actualTime=time.localtime()
        self.year=actualTime[0]
        self.month=actualTime[1]
        self.day=actualTime[2]
        self.hour=actualTime[3]
        self.minute=actualTime[4]
        self.secondi=actualTime[5]
        
    def status_operation(self):
        y = self.db.view_operation()
        self.tent=y[1]
        self.mini=y[2]
        self.boiler=y[3]
        self.normal=y[4]
        
    def status(self,id_status):
        sec_act=time.time()
        x = self.db.view_configure()
        y = self.db.view_tent()
        
        for b in x:
            id=int(b[0])
            label=b[1]
            power=b[2]
            pulse=b[6]
            switch=b[7]
            button=b[8]
            enel=b[9]
            gas=b[10]
            acqua=b[11]
           
            if id_status==id:
                if button==1:
                    if power==1:
                        self.db.update_configure('power',0,id)
                        time.sleep(2)
                    else:
                        self.db.update_configure('power',1,id)
                        time.sleep(2)
                        
                if switch==1:
                    #gestione sensore pioggia
                    if self.tent==1:
                        self.db.update_real_time('rain',1)
                    
                if pulse==1:
                    #gestione sensore vento su scheda tenda
                    #Per calcolare la velocita del vento con questo anemometro 
                    #occorre conoscere: (r) il raggio, o braccio ( distanza asse coppette in metri), il numero dei giri al secondo (f), e quindi: 
                    #Velocita =  r * 6.28 * f  = Velocita in m/s.  Per Velocita in Km/h = (Vm/s * 3,6)
                    #raggio 0.06; raggio dell'anemometro in metri
                    if self.tent==1:
                        if self.p==1:
                            self.prectime=sec_act
                        
                        diff_sec=float(sec_act)-float(self.prectime)
                        diff_imp=float(sec_act)-(self.precimp)
                        
                    
                        if diff_imp > 0.1:
                            self.p+=1
                            self.a+=1
#                        logCritical("imp 2 diff_imp %s sec_act %s precimp %s self.p %s" %(diff_imp,sec_act,self.precimp,self.p))
                        self.precimp=sec_act
                        
                        velocita=(0.06*6.28*self.p)/diff_imp
                        self.db.update_pulse('pulse4' ,velocita,id)
                    
                        if self.p >= y[5]:
                        
                            self.db.update_pulse('pulse2' ,self.p,id)
                            self.db.update_pulse('pulse3' ,self.a,id)
                        
                        
                            if diff_sec <= y[4]:
                                self.db.update_pulse('pulse%s' %id,self.p,id)
                                self.db.update_tent('mode',1)
                                self.db.update_configure('power',1,id)
                                self.db.update_configure('power',1,3)
                                self.a=0
                            #logCritical("pulse %s diff_sec %s y5 %s y4 %s" %(self.p,diff_sec,y[5],y[4]))
                            self.p=0
                            
                            
                    #gestione impulsi enel
                    #3600 (trasformazione di Ore in secondi) x 23 impulsi = 82800
                    #1000 ( imp/kWh costante GME) x 60 secondi = 60000
                    #82800 / 60000 = 1,38 kW 
                    #verifico i consumi ogni 10 minuti
                    #attualmente misuro potenza istantanea
                    
                elif self.tent==0 and enel==1:
                    diff_sec=float(sec_act)-float(self.precenel)
                    if diff_sec > 0.1:
                        a=3600/diff_sec
                        self.db.update_real_time('pulse',float(a))
                        #logCritical("diff_sec %s a %s b %s" %(diff_sec,a))
                        self.precenel=sec_act
                            
                    #gestione impulso normale su altre schede
                else:
                    self.p+=1
                    self.db.update_pulse('pulse%s' %id,self.p,id)
                         
    def run(self):
        statoreed_old=False

        while True:
            
            self.status_operation()
            self.z+=1
            
            self.gpio= getattr(self, "in%s" %self.z)
            statoreed=self.gpio.digitalRead()==0
            
            if self.tent==0:
                if self.gpio.digitalRead()==0:
                    self.status(self.z)
            else:
            #logCritical("statoreed %s" %statoreed)
                if statoreed != statoreed_old:
                    statoreed_old=statoreed
                    self.status(self.z)
            #versione mini con 2 ingressi o boiler
            if self.mini==1 or self.boiler==1:  
                if self.z==2:
                    self.z=0
            if self.tent==1:
                self.z=0
            #versione a 8 ingressi
            else: 
                if self.z==8:
                    self.z=0